<?php
namespace app\weixin\controller;
use think\Controller;
use app\weixin\model\Cart as CartModel;
class Cart extends Controller
{
//    private $_exm;
//    public function _initialize(){
//        parent::_initialize();
//        $this->_exm = new CartModel();
//    }
//    //查询购物车
//    public function selectCart()
//    {
//        $result = $this->_exm->selectCart();
//        switch($result){
//            case 0:
//                $this->response(['status'=>0,'msg'=>'购物车为空'],'json');
//                break;
//            default:
//                $this->response(['status'=>1,'data'=>$result],'json');
//                break;
//        }
//    }
      public function index(){

          return $this->fetch();
      }
      public function addCart()
      {
          //添加到购物车
          $data = file_get_contents('php://input');
          $data = json_decode($data,true);
          $uid = session('uid');
          $data['user_id'] = $uid;

          $goodsinfo = new cartModel();
          $result = $goodsinfo->insert($data);
          switch ($result)
          {
              case 0:
                  return json(['status'=>2,'data'=>'该商品已经在你的购物车中,商品数目加1'], 'json');
                  break;
              case 1:
                  return json(['status'=>1,'msg'=>'添加成功'], 'json');
                  break;
              case 2:
                  return json(['status'=>0,'msg'=>'库存不够,添加失败'], 'json');
                  break;
          }
      }
      public function selectCart()
      {
            //查询
          $goodsinfo = new cartModel();
          $result = $goodsinfo->selectCart();
          switch ($result)
          {
              case 0:
                  return json(['status'=>0,'msg'=>'购物车为空'],'json');
                  break;
              default:
                  return json(['status'=>1,'data'=>$result],'json');
                  break;
          }

      }
      //从购物车中移除
      public function removeCart(CartModel $CartModel)
      {
        $data = file_get_contents('php://input');
        dump($data);die;
        $data = json_decode($data,true);
        $id = $data['pid'];
        $result = $CartModel->removeCart($id);
        if ($result) {
          json(1);
        } else {
          json(2);
        }
        
        // switch ($result)
        // {
        //     case 1:
        //         $this->json_decode(['status'=>1,'msg'=>'移除成功'], 'json');
        //         break;
        //     default:
        //         $this->json_decode(['status'=>0,'msg'=>'移除失败'], 'json');
        //         break;
        // }
      }
}
